/**
 * @author luther.seven@gmail.com
 */

function showUserPopup()
{
    var dctk = connected.toolkit;
    
    // create a new panel for the popup
    var popup = dctk.createPanelCtrl("popup", true);
    popup.width = 240;
    popup.height = 80;
    popup.outlined = true;
    popup.filled = true;
    popup.color.rgb = 0x000000;
    popup.color.a = 192;
    popup.onUnhandledKeyEvent = popupButtonHandler;
    
    var userIdEditTxt = dctk.createLabelCtrl("userIdEditTxt", true);
    userIdEditTxt.setRect(0, 50, 82, 70);
    userIdEditTxt.textColor.rgb = 0xCDCDCD;
    userIdEditTxt.fontSize = 18;
	userIdEditTxt.align = dctkLabel.TS_RIGHT;
	userIdEditTxt.valign = dctkLabel.TS_MIDDLE;
    userIdEditTxt.text = "Joost ID:";
    popup.addChild(userIdEditTxt);
	
	var keyboardTxt = dctk.createLabelCtrl("keyboardTxt", true);
    keyboardTxt.setRect(0, 28, 240, 48);
    keyboardTxt.textColor.rgb = 0xCDCDCD;
    keyboardTxt.fontSize = 14;
	keyboardTxt.align = dctkLabel.TS_CENTER;
	keyboardTxt.valign = dctkLabel.TS_MIDDLE;
    keyboardTxt.text = "Press ENTER for the keyboard.";
    popup.addChild(keyboardTxt);

    var userIdEdit = dctk.createEditCtrl("userIdEdit", true);
    userIdEdit.setRect(87, 50, 227, 70);
    userIdEdit.text = username;
    userIdEdit.fontSize = 20;
    userIdEdit.numeric = false;
    popup.addChild(userIdEdit);
	
	var okBtn = dctk.createButtonCtrl("okBtn", true);
    okBtn.setRect(60, 10, 180, 25);
    okBtn.text = "OK";
    okBtn.fontSize = 16;
    okBtn.onSelect = function(e) { updateJoostId(); dctk.dismissPopup(); }
    popup.addChild(okBtn);
	    
    // show the popup
    dctk.invokePopup(popup);
    
	// set siblings
	userIdEdit.bottomSibling = okBtn;
	okBtn.topSibling = userIdEdit;
	
	dctk.focusedElement = userIdEdit;
}

function popupButtonHandler(e)
{
    var dctk = connected.toolkit;

    switch(e.buttonId)
    {
        case dcKeyEvent.BUTTON_ID_BACK:
        {
            dctk.dismissPopup();
        }
        break;

        case dcKeyEvent.BUTTON_ID_SELECT:
        {
            fetchNewSearchResults();
            dctk.dismissPopup();
        }
        break;
    }
}

function updateJoostId()
{
    var dctk = connected.toolkit;
	var userIdEdit = dctk.getElementById("userIdEdit");
	
	var inputUserId = userIdEdit.text;
	
	var storage = connected.openStorage(joostStorageUn,true);
	storage.set(joostUserId,inputUserId);
	
	onLoadFavorites();
}



